package com.sg.business.permission.adapter;

import org.bson.types.ObjectId;
import org.eclipse.core.runtime.IAdapterFactory;

import com.mobnut.db.model.ModelService;
import com.sg.business.model.Folder;
import com.sg.business.model.FolderDefinition;
import com.sg.business.permission.adapter.doc.FolderACL;
import com.sg.business.permission.adapter.docdef.FolderDefACL;
import com.sg.business.permission.model.ACLControlableContainerDelgator;
import com.sg.business.permission.model.IACLControlableContainer;

public class ACLContainerAdapterFactory implements IAdapterFactory {
	
	public static final String TYPE_FOLDER = "folder";
	public static final String TYPE_FOLDER_DEFINITION = "folderdef";

	@SuppressWarnings("rawtypes")
	@Override
	public Object getAdapter(Object adaptableObject, Class adapterType) {
		if (adapterType == IACLControlableContainer.class) {
			if (adaptableObject instanceof ACLControlableContainerDelgator) {
				ACLControlableContainerDelgator delgator = (ACLControlableContainerDelgator) adaptableObject;
				ObjectId id = delgator.getId();
				String type = delgator.getType();
				if(type.equals(TYPE_FOLDER)){
					Folder folder = ModelService.createModelObject(Folder.class, id);
					return new FolderACL(folder);
				}else if(type.endsWith(TYPE_FOLDER_DEFINITION)){
					FolderDefinition folder = ModelService.createModelObject(FolderDefinition.class, id);
					return new FolderDefACL(folder);
				}
			}
		}
		return null;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Class[] getAdapterList() {
		return new Class[] { IACLControlableContainer.class };
	}

}
